<template>
	<uv-popup ref="popupRef" mode="bottom" closeable round="28rpx">
		<view class="platform-dia">
			<view class="title">选择平台</view>
			<view class="btns">
				<button class="cu-btn" :class="{ 'cu-btn-active': el.checked }" v-for="(el, n) in allList" :key="el.id"
					@click="handSelect(el)">
					{{ el.name }}
				</button>
			</view>
			<view class="submit">
				<button class="cu-btn" @click="handSubmit">确定</button>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		reactive,
		ref,
		defineEmits
	} from 'vue'
	import {
		doPlatform
	} from '@/apis/system.js'
	const emit = defineEmits(['change'])
	const popupRef = ref(null)
	const allList = ref([])
	const handOpen = async (arr) => {
		const data = await doPlatform()
		allList.value = data.map(el => {
			el.checked = arr.includes(el.name)
			return el
		})
		popupRef.value.open()
	}
	const handSubmit = () => {
		const arr = allList.value.filter(el => el.checked).map(el => {
			return el.name
		})
		if (arr.length === 0) {
			uni.$uv.toast('请至少选择一个平台')
			return
		}
		emit('change', arr.toString())
		handClose()
	}
	const handClose = () => {
		popupRef.value.close()
	}
	const handSelect = (e) => {
		e.checked = !e.checked
	}
	defineExpose({
		handOpen
	})
</script>

<style lang="scss" scoped>
	.platform-dia {
		padding: 20rpx 24rpx 40rpx 24rpx;
		padding-bottom: 24rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 24rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 24rpx);

		.title {
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
		}

		.btns {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			margin-top: 50rpx;

			.cu-btn {
				height: 68rpx;
				background: #F5F5F5;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				margin: 0;
				border: 1rpx solid #F5F5F5;

				&::after {
					border: none;
				}

				&-active {
					background: #FFF7E4;
					color: #FF930D;
					border-color: #FF930D;
				}
			}
		}

		.submit {
			margin-top: 200rpx;
			display: flex;
			justify-content: center;

			.cu-btn {
				width: 100%;
				height: 88rpx;
				background-color: #FF930D;
				color: #fff;
			}
		}
	}
</style>